package com.cloud.pole.shop.controller.goods;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cloud.pole.common.constant.CommonConstants;
import com.cloud.pole.common.exception.BaseException;
import com.cloud.pole.common.exception.ParamException;
import com.cloud.pole.common.msg.ApiRestResponse;
import com.cloud.pole.common.util.RegexUtils;
import com.cloud.pole.shop.model.entity.goods.ShopGoods;
import com.cloud.pole.shop.model.entity.order.ShopOrderComment;
import com.cloud.pole.shop.model.vo.goods.GoodsDetailVo;
import com.cloud.pole.shop.model.vo.goods.ShopGoodsVo;
import com.cloud.pole.shop.model.vo.order.ShopOrderCommentVo;
import com.cloud.pole.shop.service.goods.GoodsService;
import com.cloud.pole.shop.service.order.OrderCommentService;
import com.github.pagehelper.PageInfo;

import cn.hutool.core.bean.BeanUtil;
import lombok.extern.slf4j.Slf4j;

/*
*2019年2月21日
*cloud-shop
*author:frank-chen
*/
@Controller
@Slf4j
@RequestMapping("/goods/front")
public class GoodsController {

	@Autowired
	private GoodsService goodsService;

	@Autowired
	private OrderCommentService orderCommentService;
	/**
	 * 查询产品列表
	 * @param sortField
	 * @param sortOrder
	 * @param shopId
	 * @param categoryId
	 * @param code
	 * @param keyword
	 * @param pageSize
	 * @param pageNum
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/selectList")
    public ApiRestResponse<List<ShopGoodsVo>> selectList(
    		@RequestHeader(defaultValue = "1") String shopId,
    		@RequestParam(defaultValue = "") String sortField,
    		@RequestParam(defaultValue = "") String sortOrder,
    		@RequestParam(defaultValue = "") String groupId,
    		@RequestParam(defaultValue = "") String code,
    		@RequestParam(defaultValue = "") String keyword,
    		@RequestParam(defaultValue = "") String pageSize,
    		@RequestParam(defaultValue = "") String pageNum){   	
    	try {
			ParamException.err(StringUtils.isBlank(shopId), "shopId不能为空");
			ParamException.err(StringUtils.isBlank(pageSize), "pageSize不能为空");
			ParamException.err(StringUtils.isBlank(pageNum), "pageNum不能为空");
			ParamException.err(!RegexUtils.isInteger(pageSize), "pageSize错误");
			ParamException.err(!RegexUtils.isInteger(pageNum), "pageNum错误");
    		if(!StringUtils.isEmpty(groupId)) {
				ParamException.err(!RegexUtils.isInteger(groupId), "groupId错误");
			}
			if(!StringUtils.isEmpty(shopId)) {
				ParamException.err(!RegexUtils.isInteger(shopId), "shopId错误");
			}
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("sortField", sortField);
			map.put("sortOrder", sortOrder);
			map.put("shopId", shopId);
			map.put("groupId", groupId);
			map.put("code", code);
			map.put("keyWord", keyword);
			
			PageInfo<ShopGoods> pageInfo = goodsService.selectPageShopGoodsList(map, Integer.parseInt(pageSize), Integer.parseInt(pageNum));
			List<ShopGoods> listGoods = pageInfo.getList();
			List<ShopGoodsVo> listGoodsVo = new ArrayList<ShopGoodsVo>();
			if(listGoods != null) {
				for(ShopGoods shopGoods : listGoods) {
					ShopGoodsVo shopGoodsVo = new ShopGoodsVo(); 
					BeanUtil.copyProperties(shopGoods, shopGoodsVo);
					listGoodsVo.add(shopGoodsVo);
				}
			}
			return ApiRestResponse.successOfPage(CommonConstants.STATUS_OK_MESSAGE, (int)pageInfo.getTotal(), pageInfo.getPageNum(), pageInfo.getPageSize(), listGoodsVo);
    	} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }
	
	@ResponseBody
	@RequestMapping("/getGoodsDetail")
    public ApiRestResponse<GoodsDetailVo> getGoodsDetail(
    		@RequestHeader(defaultValue = "") String token,
    		@RequestParam(defaultValue = "") String goodsId,
    		@RequestParam(defaultValue = "") String specId,
    		@RequestParam(defaultValue = "") String recommandMarketCode){   	
    	try {
			ParamException.err(StringUtils.isBlank(goodsId), "goodsId不能为空");
			ParamException.err(!RegexUtils.isInteger(goodsId), "goodsId错误");
			if(!StringUtils.isEmpty(specId)) {
				ParamException.err(!RegexUtils.isInteger(specId), "specId错误");
			}
			return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, goodsService.getGoodsDetail(token, goodsId, specId, recommandMarketCode));
    	} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }
	
	@ResponseBody
	@RequestMapping("/addCollection")
    public ApiRestResponse<Boolean> addCollection(
    		@RequestHeader(defaultValue = "") String token,
    		@RequestParam(defaultValue = "") String goodsId){   	
    	try {
			ParamException.err(StringUtils.isBlank(token), "token不能为空");    		
			ParamException.err(StringUtils.isBlank(goodsId), "goodsId不能为空");
			ParamException.err(!RegexUtils.isInteger(goodsId), "goodsId错误");
			return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, goodsService.addCollection(token, goodsId));
    	} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }
	
	@ResponseBody
	@RequestMapping("/cancelCollection")
    public ApiRestResponse<Boolean> cancelCollection(
    		@RequestHeader(defaultValue = "") String token,
    		@RequestParam(defaultValue = "") String goodsId){   	
    	try {
			ParamException.err(StringUtils.isBlank(token), "token不能为空");    		
			ParamException.err(StringUtils.isBlank(goodsId), "goodsId不能为空");
			ParamException.err(!RegexUtils.isInteger(goodsId), "goodsId错误");
			return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, goodsService.cancelCollection(token, goodsId));
    	} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }
	
	@ResponseBody
	@RequestMapping("/getGoodsCommentList")
    public ApiRestResponse<List<ShopOrderCommentVo>> getGoodsCommentList(
    		@RequestParam(name="commentType", defaultValue="") String commentType,
    		@RequestParam(name="pageSize", defaultValue="") String pageSize,
    		@RequestParam(name="pageNum", defaultValue="") String pageNum,
    		@RequestParam(name="goodsId", defaultValue="") String goodsId){   	
    	try {
			ParamException.err(StringUtils.isBlank(goodsId), "goodsId不能为空");
			ParamException.err(!RegexUtils.isPositiveInteger(goodsId), "goodsId错误");

			ParamException.err(StringUtils.isBlank(pageSize), "pageSize不能为空");
			ParamException.err(!RegexUtils.isPositiveInteger(pageSize), "pageSize错误");
			
			ParamException.err(StringUtils.isBlank(pageNum), "pageNum不能为空");
			ParamException.err(!RegexUtils.isPositiveInteger(pageNum), "pageNum错误");

			PageInfo<ShopOrderComment> pageInfo = orderCommentService.getPageOrderCommentList(commentType, Integer.parseInt(goodsId), Integer.parseInt(pageSize), Integer.parseInt(pageNum));
			List<ShopOrderComment> list = pageInfo.getList();
			List<ShopOrderCommentVo> listVo = new ArrayList<ShopOrderCommentVo>();
			if(list != null) {
				for(ShopOrderComment srcMsg : list) {
					log.info(srcMsg.toString());
					ShopOrderCommentVo descMsg = new ShopOrderCommentVo(); 
					BeanUtil.copyProperties(srcMsg, descMsg);
					listVo.add(descMsg);
				}
			}
			return ApiRestResponse.successOfPage(CommonConstants.STATUS_OK_MESSAGE, (int)pageInfo.getTotal(), pageInfo.getPageNum(), pageInfo.getPageSize(), listVo);
      	} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }
	
	/**
	 * 查询移动充值产品
	 * @param shopId
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getRechargeGoods")
    public ApiRestResponse<GoodsDetailVo> getRechargeGoods(
    		@RequestHeader(defaultValue = "1") String shopId){   	
    	try {
			return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, goodsService.getRechargeGoods(shopId));
    	} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }
}
